package com.huluobo.lc.a1803aretrofitrxdemo.mvp;

import com.huluobo.lc.a1803aretrofitrxdemo.network.response.ResponseTransformer;
import com.huluobo.lc.a1803aretrofitrxdemo.network.schedulers.BaseSchedulerProvider;

import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @author Lc
 * @description:
 * @date :2020/11/10 15:27
 */
class Presenter implements Contract.Presenter {
    private Contract.Model model;

    private Contract.View view;

    private BaseSchedulerProvider schedulerProvider;

    private CompositeDisposable compositeDisposable;//内存泄漏

    public Presenter(Contract.Model model, Contract.View view,
                     BaseSchedulerProvider schedulerProvider) {
        this.model = model;
        this.view = view;
        this.schedulerProvider = schedulerProvider;
        compositeDisposable = new CompositeDisposable();
    }

    public void destroy() {
        compositeDisposable.dispose();//取消掉当前网络请求,防止内存泄漏
    }

    @Override
    public void getCarList(String userId) {
        Disposable disposable =
                model.getCartList(userId).compose(ResponseTransformer.handleResult()).compose(schedulerProvider.applySchedulers()).subscribe(carBeans -> {
            //处理数据
            view.getDataSuccess();
        }, throwable -> {
            //处理异常
            view.getDataFailed();
        });
        compositeDisposable.add(disposable);//添加进防止内存泄漏
    }
}
